Skip to content

Conversation

nosan
Copy link
Contributor

@nosan nosan commented Aug 23, 2025

See #35369

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 23, 2025
@nosan nosan force-pushed the 35369 branch 2 times, most recently from a722667 to a596879 Compare August 23, 2025 22:25
private static final Object NULL = new Object() {

@Override
public String toString() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that overriding this method is not necessary, what's the purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason that the method is overridden isn't for functionality but for debugging purposes.

Screenshot 2025-08-25 at 10 08 17

When overridden:
Screenshot 2025-08-25 at 10 07 57

Previously, SingletonSupplier stored "null" in singletonInstance when
the supplied instance was "null". On subsequent get() calls, this was
treated as "uninitialized" and triggered another attempt to obtain an instance.

This commit ensures that a "null" returned from the instanceSupplier or
defaultSupplier is handled correctly, so that subsequent calls to get()
return "null" consistently instead of repeatedly invoking the supplier.

Signed-off-by: Dmytro Nosan <[email protected]>

reset(dataSource);
given(dataSource.getConnection()).willReturn(connection);
translator = new SQLErrorCodeSQLExceptionTranslator(dataSource);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related to this issue/change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

public @Nullable T get() {
T instance = this.singletonInstance;
if (instance == null) {
if (!this.initialized) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as initialized has no external use, is this variable really necessary? cant we directly use this.singletonInstance == null or add a function->

	public boolean isInitialized() {
		return Objects.nonNull(this.singletonInstance);
     }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this.singletonInstance == null can't tell the difference between "not initialized yet" and "initialized but supplier returned null." Since the supplier can return null, a separate flag needs to be used to record that initialization was attempted. Otherwise, the supplier will keep getting re-invoked.

See #35369 for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants